package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bsh.dto.PartnerListQryDto;
import com.bsh.mybatis.dataView.PartnerExt;
import com.bsh.mybatis.model.Partner;
import com.bsh.repository.DaoPartner;
import com.bsh.repository.DaoUser;
import com.bsh.service.AccountService;
import com.bsh.service.PartnerService;
import com.bsh.utils.Pager;

/**
 * 合作伙伴
 * @author gsh
 *
 */
@Service
@Transactional
public class PartnerServiceImpl implements PartnerService {
	@Autowired
	private DaoPartner partDao;
	@Autowired
	private DaoUser daoUser;
	@Autowired
	private AccountService servAcc;

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.PartnerService#getPage(com.bsh.utils.Pager, com.bsh.dto.PartnerListQryDto)
	 */
	@Override
	public List<Map<String, Object>> getPage(Pager pager,PartnerListQryDto qryParams) {
		StringBuilder sb =new StringBuilder();
		if(!StringUtils.isEmpty(qryParams.getPartnerLevel()))
			sb.append(String.format(" and v.partner_level=%s ",qryParams.getPartnerLevel().trim()));
		if(!StringUtils.isEmpty(qryParams.getUserName()))
			sb.append(String.format(" and u.name like '%%%s%%' ",qryParams.getUserName().trim()));
		if(!StringUtils.isEmpty(qryParams.getMobile()))
			sb.append(String.format(" and u.mobile like '%%%s%%' ",qryParams.getMobile().trim()));	
		if(!StringUtils.isEmpty(qryParams.getRealName()))
			sb.append(String.format(" and v.real_name like '%%%s%%' ",qryParams.getRealName().trim()));	
		if(!StringUtils.isEmpty(qryParams.getCreateTime()) && StringUtils.isEmpty(qryParams.getCreateTime1())){
			sb.append(String.format(" and v.create_time >='%s' ",qryParams.getCreateTime().trim()));
		}else if(StringUtils.isEmpty(qryParams.getCreateTime()) && !StringUtils.isEmpty(qryParams.getCreateTime1())){
			sb.append(String.format(" and v.create_time <='%s 23:59:59' ",qryParams.getCreateTime1().trim()));
		}else if(!StringUtils.isEmpty(qryParams.getCreateTime()) && !StringUtils.isEmpty(qryParams.getCreateTime1())){
			sb.append(String.format(" and (v.create_time between '%s' and '%s 23:59:59') ",qryParams.getCreateTime().trim(),qryParams.getCreateTime1().trim()));
		}
		
		List<Map<String, Object>> list = partDao.getPage(pager,sb.toString());
		return list;
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.PartnerService#getAskPage(com.bsh.utils.Pager, java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getAskPage(Pager pager,String userName,String mobile) {
		StringBuilder sb =new StringBuilder();
		if(!StringUtils.isEmpty(userName))
			sb.append(String.format(" and u.name like '%%%s%%'",userName.trim()));
		if(!StringUtils.isEmpty(mobile))
			sb.append(String.format(" and u.mobile like '%%%s%%'",mobile.trim()));		
		List<Map<String, Object>> list = partDao.getAskPage(pager,sb.toString());
		return list;
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.PartnerService#addPartner(com.bsh.mybatis.model.Partner)
	 */
	@Override
	public Partner addPartner(Partner partnerReq) {
		int id= partDao.addPartner(partnerReq);
		Partner pr = partDao.selectByPrimaryKey(id);
		return pr;
	}
	
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.PartnerService#audit(int, boolean, java.lang.String)
	 */
	@Override
	public int audit(int partnerId,boolean isPass,String auditMsg,String ip) {
		if(isPass){
		  Partner info = partDao.getPartnerInfo(partnerId);
		  
		  //检测用户在提交区域的合伙人级别
		  Partner part = partDao.checkInAreaLevel(info.getUserid(),info.getCountry(),info.getProvince(),info.getCity(),info.getArea(),info.getPartnerLevel());
		  if(part!=null){
			  return -1; //该地区已存在相同级别
		  }
		  
		  //通过
		  int res = partDao.audit(partnerId, true, "");
		  
		  int level=info.getPartnerLevel();
		  if(level>0){
		    //加商币
		    servAcc.partnerNewAddCloudMoney(partnerId,ip);
		    
		    //更新用户表合伙级别  
		    daoUser.setUserPartnerLevel(info.getUserid(), level);
		  }
		  return res;
		}
		//不过通
		return partDao.audit(partnerId, false, auditMsg);
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.PartnerService#getPartner(int)
	 */
	@Override
	public Partner getPartner(int mid) {
		Partner mt = partDao.selectByPrimaryKey(mid);
		return mt;
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.PartnerService#editPartner(com.bsh.mybatis.model.Partner)
	 */
	@Override
	public int editPartner(Partner partner) {
		int mt = partDao.editPartner(partner);
		if(mt>0){
			Partner info = partDao.selectByPrimaryKey(partner.getId());
			if(info.getStatus()==1){
				  daoUser.setUserPartnerLevel(info.getUserid(), info.getPartnerLevel());
			}
		}
		return mt;
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.PartnerService#getPartnerInfo(int)
	 */
	@Override
	public PartnerExt getPartnerInfo(int pid) {
		PartnerExt mt = partDao.getPartnerInfo(pid);
		return mt;
	}

	@Override
	public List<Map<String, Object>> getPartnerByUserid(Integer userId) {
		return partDao.getPartnerByUserid(userId);
	}

	@Override
	public Integer disable(int partnerId) {
		return partDao.disable(partnerId);
	}
	
	@Override
	public List<Map<String, Object>> getRemovedPage(Pager pager,PartnerListQryDto qryParams) {
		StringBuilder sb =new StringBuilder();
		if(!StringUtils.isEmpty(qryParams.getPartnerLevel()))
			sb.append(String.format(" and v.partner_level=%s ",qryParams.getPartnerLevel().trim()));
		if(!StringUtils.isEmpty(qryParams.getUserName()))
			sb.append(String.format(" and u.name like '%%%s%%' ",qryParams.getUserName().trim()));
		if(!StringUtils.isEmpty(qryParams.getMobile()))
			sb.append(String.format(" and u.mobile like '%%%s%%' ",qryParams.getMobile().trim()));	
		if(!StringUtils.isEmpty(qryParams.getRealName()))
			sb.append(String.format(" and v.real_name like '%%%s%%' ",qryParams.getRealName().trim()));	
		if(!StringUtils.isEmpty(qryParams.getCreateTime()) && StringUtils.isEmpty(qryParams.getCreateTime1())){
			sb.append(String.format(" and v.create_time >='%s' ",qryParams.getCreateTime().trim()));
		}else if(StringUtils.isEmpty(qryParams.getCreateTime()) && !StringUtils.isEmpty(qryParams.getCreateTime1())){
			sb.append(String.format(" and v.create_time <='%s 23:59:59' ",qryParams.getCreateTime1().trim()));
		}else if(!StringUtils.isEmpty(qryParams.getCreateTime()) && !StringUtils.isEmpty(qryParams.getCreateTime1())){
			sb.append(String.format(" and (v.create_time between '%s' and '%s 23:59:59') ",qryParams.getCreateTime().trim(),qryParams.getCreateTime1().trim()));
		}
		
		List<Map<String, Object>> list = partDao.getRemovedPage(pager,sb.toString());
		return list;
	}
}
